home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 2: CDPD 1 / Almathera Ten on Ten - Disc 2: CDPD 1.iso / pd / 076-100 / 079 / sharedlib / task_routines.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  3KB  |  112 lines

  1. /* Task_Routines.c    Copyright 1987 by James M Synge   */
  2.  
  3. /* This file contains CreateTask() and DeleteTask(),
  4.  * versions of the routines of the same names in the ROM
  5.  * Kernel Manual.
  6.  */
  7.  
  8. #include "exec/types.h"
  9. #include "exec/ports.h"
  10. #include "exec/tasks.h"
  11. #include "exec/memory.h"
  12.  
  13. struct Task *
  14. CreateTask(Task_Name, Priority, Startup_Routine, Stack_Size)
  15.  
  16. char *Task_Name;
  17. int Priority;
  18. void (* Startup_Routine) ();
  19. int Stack_Size;
  20. {
  21.     /* A pointer to the child task's task structure. */
  22.     register struct Task *Child;
  23.     register APTR Child_Stack;   /* A pointer to it's stack */
  24.     register APTR AllocMem();
  25.  
  26.     /* First allocate a stack for the task. */
  27.  
  28.     Child_Stack = AllocMem(Stack_Size, MEMF_CLEAR);
  29.  
  30.     if (Child_Stack == 0) {
  31.  
  32. /*    Couldn't allocate a stack! And we cann't print an
  33.  *    error message because this routine could be called
  34.  *    by a task or a process which doesn't have a window.
  35.  */
  36.     return(NULL);
  37.     };
  38.  
  39.     /* Now allocate a Task structure. */
  40.  
  41.     Child = (struct Task *)
  42.         AllocMem(sizeof(struct Task),
  43.          MEMF_CLEAR | MEMF_PUBLIC);
  44.  
  45.     if (Child == NULL) {
  46.     FreeMem(Child_Stack, Stack_Size);
  47.     return(NULL);
  48.     };
  49.  
  50.     /* Now initialize the task structure as per the RKM. */
  51.  
  52.     Child->tc_SPLower = Child_Stack;
  53.  
  54.     Child->tc_SPReg   =
  55.     Child->tc_SPUpper = (APTR)( (ULONG)Child_Stack +
  56.                     (ULONG)Stack_Size);
  57.  
  58.     Child->tc_Node.ln_Type = NT_TASK;
  59.     Child->tc_Node.ln_Pri = Priority;
  60.     Child->tc_Node.ln_Name = Task_Name;
  61.  
  62.     AddTask(Child, Startup_Routine, 0L);
  63.  
  64.     return(Child);
  65. }
  66.  
  67. /* DeleteTask is written in a style which will allow it to
  68.  * be called by any task, including the task being deleted.
  69.  * Note that it can not delete an AmigaDOS Process.
  70.  */
  71.  
  72. void DeleteTask(Child)
  73.     register struct Task *Child;
  74. {
  75.     register ULONG Stack_Size;
  76.  
  77.     if (Child == NULL) return;
  78.  
  79.     /* Free up the stack and the Task structure.  This is
  80.      * done inside a Forbid() / Permit() so that no other
  81.      * task can do the same.
  82.      */
  83.  
  84.     Forbid();
  85.  
  86.     if (Child->tc_Node.ln_Type != NT_TASK) {
  87.     /* OOPS! It ain't a Task! */
  88.     Permit();
  89.         return;
  90.     }
  91.  
  92.     Stack_Size = (ULONG)(Child->tc_SPUpper) -
  93.              (ULONG)(Child->tc_SPLower);
  94.  
  95.     FreeMem(Child->tc_SPLower, Stack_Size);
  96.     FreeMem(Child, sizeof(struct Task));
  97.  
  98.     /* Now remove the task.  This will not return if it is
  99.      * removing the current task.
  100.      */
  101.  
  102.     RemTask( Child );
  103.  
  104.     /* If we removed another task, then we continue on:
  105.      * so call Permit() and return.
  106.      */
  107.  
  108.     Permit();
  109.  
  110.     return;
  111. }
  112.